package com.honey.solr.schema.node;

import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import com.honey.compilation.xml.XmlElement;
import com.honey.core.calculator.JavaNameCalculator;
import com.honey.solr.SchemaField;
import com.honey.solr.SolrConstant;
import com.honey.solr.SolrFactory;
import com.honey.solr.schema.SchemaNode;

public class Fields implements SchemaNode {
	
	private SolrFactory solrFactory;
	
	public Fields(SolrFactory solrFactory){
		this.solrFactory = solrFactory;
	}
	
	@Override
	public void appendChildNode(XmlElement root){
		XmlElement answer = new XmlElement(root,"fields");
		answer.addXmlDoc("索引列");
		List<SchemaField> list = solrFactory.getSchemaFields();
		if ( list != null && list.size()>0 ){
			addBaseColumns(answer,list);
		}
		addAllField(answer);
		answer.addNewLine();
		addDynamicField(answer);
	}
	
	private void addBaseColumns(XmlElement parent ,List<SchemaField> list){
		JavaNameCalculator  javaNameCalculator= solrFactory.getJavaNameCalculator() ;
		for(SchemaField field : list){
			parent.addComments(field.getComment());
			XmlElement node= new XmlElement(parent, "field");
			node.addAttribute("name", javaNameCalculator.fieldNameCalculator( field.getName() ));
			if( field.isUniqueKey() ){
				node.addAttribute("type", field.getType().getSolrPrimitiveType())
				.addAttribute("indexed", "true")
				;
			}else{
				node
				.addAttribute("type", field.getType().isText() ? solrFactory.getSolrTextType() : field.getType().getSolrType())
				.addAttribute("indexed", String.valueOf(field.isIndexed()))
				;
			}
			boolean b = field.isRequired();
			node.addAttribute("stored", b?"true" : String.valueOf( field.isStored()))
			.addAttribute("required", String.valueOf(b))
			;
			Properties p = field.getProperties();
			if(p.size()>0){
				Iterator<Object> s = p.keySet().iterator();
				while( s.hasNext()){
					Object key = s.next();
					node.addAttribute(String.valueOf(key), String.valueOf(p.get(key)));
				}
			}
		}
	}
	
	private void addAllField(XmlElement parent ){
		parent.addComments("拷贝字段, 将所有的文本字段复制到一个字段中,以便进行统一检索.");
		new XmlElement(parent, "field")
		.addAttribute("name",SolrConstant.SOLR_SCHEMA_COPY_FIELD_NAME)
		.addAttribute("type", solrFactory.getSolrTextType() )
		.addAttribute("indexed","true")
		.addAttribute("stored", "false")
		.addAttribute("multiValued", "true")
		;
	}
	private void addDynamicField(XmlElement parent ){
		parent.addComments("动态字段,没有具体名称的字段.");
		new XmlElement(parent,"dynamicField")
		.addAttribute("name","*_i")
		.addAttribute("type","int")
		.addAttribute("indexed","true")
		.addAttribute("stored", "true")
		;
		new XmlElement(parent,"dynamicField")
		.addAttribute("name","*_s")
		.addAttribute("type","string")
		.addAttribute("indexed","true")
		.addAttribute("stored", "true")
		;
		new XmlElement(parent,"dynamicField")
		.addAttribute("name","*_l")
		.addAttribute("type","long")
		.addAttribute("indexed","true")
		.addAttribute("stored", "true")
		;
		new XmlElement(parent,"dynamicField")
		.addAttribute("name","*_t")
		.addAttribute("type",solrFactory.getSolrTextType())
		.addAttribute("indexed","true")
		.addAttribute("stored", "true")
		;
		new XmlElement(parent,"dynamicField")
		.addAttribute("name","*_b")
		.addAttribute("type","boolean")
		.addAttribute("indexed","true")
		.addAttribute("stored", "true")
		;
		new XmlElement(parent,"dynamicField")
		.addAttribute("name","*_f")
		.addAttribute("type","float")
		.addAttribute("indexed","true")
		.addAttribute("stored", "true")
		;
		new XmlElement(parent,"dynamicField")
		.addAttribute("name","*_d")
		.addAttribute("type","double")
		.addAttribute("indexed","true")
		.addAttribute("stored", "true")
		;
		new XmlElement(parent,"dynamicField")
		.addAttribute("name","*_dt")
		.addAttribute("type","date")
		.addAttribute("indexed","true")
		.addAttribute("stored", "true")
		;
		
	}
}
